package com.suchuang.dcs.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.suchuang.dcs.dao.${bean_type_name}Mapper;
import com.suchuang.dcs.po.${bean_type_name};
import com.suchuang.dcs.po.${bean_type_name}Example;
import com.suchuang.dcs.po.${bean_type_name}Param;
import com.suchuang.dcs.util.UUIDUtil;
import com.suchuang.dcs.util._NumberUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * ${table.TABLE_COMMENT}
 * @author rengy
 */
@Service
@Slf4j
public class Front${bean_type_name}Service {
    
    
    @Autowired
    private ${bean_type_name}Mapper ${bean_name}Mapper;
    
    /**
     * 查询
     * @param param
     * @return
     */
    public List<${bean_type_name}> select(${bean_type_name}Param param){
    	log.debug(param.toString());
    	boolean isPaging=param.getIsPaging();
    	if(isPaging){
    		int pageIndex=_NumberUtils.toInt(param.getPageIndex(), 1);
    		int pageSize=_NumberUtils.toInt(param.getPageSize(), 10);
    		PageHelper.startPage(pageIndex, pageSize);
    	}
    	
		${bean_type_name}Example example = new ${bean_type_name}Example();
		<#list columnList as column>
			<#if column.COLUMN_NAME=="create_time">
		example.setOrderByClause("create_time desc");
			</#if>
		</#list>
		${bean_type_name}Example.Criteria criteria = example.createCriteria();
		
		<#list columnList as column>
			<#if column.DATA_TYPE=="varchar">
			if(param.get${column.bean_type_name}()!=null&&param.get${column.bean_type_name}().length()>0){
				//criteria.and${column.bean_type_name}Like(param.get${column.bean_type_name}());
				criteria.and${column.bean_type_name}EqualTo(param.get${column.bean_type_name}());
			}
			<#elseif column.DATA_TYPE=="int">
			if(param.get${column.bean_type_name}()!=null){
				criteria.and${column.bean_type_name}EqualTo(param.get${column.bean_type_name}());
			}
			<#elseif column.DATA_TYPE=="datetime">
			if(param.get${column.bean_type_name}Start()!=null){
				criteria.andCreateTimeBetween(param.get${column.bean_type_name}Start(), param.get${column.bean_type_name}End());
			}
			<#else>
								
			</#if>
		</#list>
		List<${bean_type_name}> list=${bean_name}Mapper.selectByExample(example);
    	return list;
    }
    
    
	/**
	<#list columnList as column>
	${bean_type_name} ${bean_name}=new ${bean_type_name}();
	${bean_name}.set${column.bean_type_name}();//${column.COLUMN_COMMENT}
	</#list>
	**/
	public int insert(${bean_type_name} param) {
		Date now=new Date();
		param.setId(UUIDUtil.get32RanID());
		<#list columnList as column>
			<#if column.COLUMN_NAME=="create_time">
		param.setCreateTime(now);
			</#if>
			<#if column.COLUMN_NAME=="is_deleted">
		param.setIsDeleted(0);
			</#if>
			<#if column.COLUMN_NAME=="is_disabled">
		param.setIsDisabled(0);
			</#if>
			<#if column.COLUMN_NAME=="status">
		param.setStatus(0);
			</#if>
			<#if column.COLUMN_NAME=="creator_id">
		param.setCreatorId(param.getUserId());
			</#if>
			<#if column.COLUMN_NAME=="creator_name">
		param.setCreatorName(param.getRealName());
			</#if>
			<#if column.COLUMN_NAME=="update_time">
		param.setUpdateTime(now);
			</#if>
		</#list>
		int i=${bean_name}Mapper.insert(param);
		return i;
	}

	public ${bean_type_name} get(String id) {
		${bean_type_name} record=${bean_name}Mapper.selectByPrimaryKey(id);
		return record;
	}

	
	
	



	public long countByExample(${bean_type_name}Example example) {
		long i=${bean_name}Mapper.countByExample(example);
		return i;
	}




	public int deleteByExample(${bean_type_name}Example example) {
		int i=${bean_name}Mapper.deleteByExample(example);
		return i;
	}




	public int deleteByPrimaryKey(String id) {
		int i=${bean_name}Mapper.deleteByPrimaryKey(id);
		return i;
	}




//	public int insertSelective(DcsDocumentReceiver record) {
//		int i=dcsDocumentReceiverMapper.insertSelective(record);
//		return i;
//	}



	public ${bean_type_name} selectByPrimaryKey(String id) {
		${bean_type_name} entity=${bean_name}Mapper.selectByPrimaryKey(id);
		return entity;
	}



	/**
	 * 根据指定条件，更新参数不为空字段，
	 * @param record
	 * @param example
	 * @return
	 */
	public int updateByExampleSelective(${bean_type_name} record, ${bean_type_name}Example example) {
		int i=${bean_name}Mapper.updateByExampleSelective(record, example);
		return i;
	}



	



	/**
	 * 根据ID更新参数不为空字段
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKeySelective(${bean_type_name} record) {
		
		return ${bean_name}Mapper.updateByPrimaryKeySelective(record);
	}




	public int updateByPrimaryKey(${bean_type_name} record) {
		//先查询再更新
		//${bean_type_name} entity=${bean_name}Mapper.selectByPrimaryKey(record.getId());
		
		//return ${bean_name}Mapper.updateByPrimaryKey(entity);
		return 0;
	}
    
    
}
